new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Sales_Receipt_SerialNumber: '',//主键
                Receipt_Date: '',//出库日期
                Receipt_Type: "0",//出货地点
                Receipt_Name: "",
                Store_SerialNumber: "",//仓库编号
                Store_Name: "",
                Enterprise_Info_SerialNumber: "",//企业主键
                Children: {
                    sales_receipt_detail: [
                        {
                            Sales_Receipt_Detail_SerialNumber: "",//主键
                            Sales_Receipt_SerialNumber: "",//外键
                            Order_Type: "1",//订单类型
                            Sales_Order_SerialNumber: "",//销售订单流水号
                            Article_SerialNumber: "",//商品流水号
                            Article_Name: "",
                            Receipt_Quantity: "",//出库数量
                            // Amount:"",//商品单价
                            Order_Name: "",
                            Logistics_Number: "",//物流单号
                        }
                    ]
                }
            },
            purchaseOrderData: {},
            salesOrderData: {},
            formData: {},
            loading: false,
            disabled: true
        }
    },
    mounted() {
        this.formData = JSON.parse(sessionStorage.getItem('fillorder'))
        console.log(this.formData)
        this.form.Receipt_Date = getYMDHMSDate()
        this.form.Receipt_Type == '0' ? this.form.Receipt_Name = '仓库' : this.form.Receipt_Name = '店铺'
        this.form.Children.sales_receipt_detail[0].Order_Type == '1' ? this.form.Children.sales_receipt_detail[0].Order_Name = '企业' : this.form.Children.sales_receipt_detail[0].Order_Name = '用户'
        this.form.Store_Name = this.formData.Warehouse_Name
        this.form.Enterprise_Info_SerialNumber = this.formData.Enterprise_Info_SerialNumber
        this.form.Children.sales_receipt_detail[0].Sales_Order_SerialNumber = this.formData.Purchase_Order_SerialNumber
        this.form.Children.sales_receipt_detail[0].Article_Name = this.formData.Article_Name
        this.form.Children.sales_receipt_detail[0].Receipt_Quantity = this.formData.Delivery_Quantity
        if (this.formData.Purchase_Order_SerialNumber.indexOf("M") != -1) {
            this.purchaseOrder(this.formData.Purchase_Order_SerialNumber)
        } else {
            console.log(this.formData.Purchase_Order_SerialNumber);
            // this.salesOrder(this.formData.Purchase_Order_SerialNumber)
        }
       
        if (this.form.Children.sales_receipt_detail.length != 0) {
            this.disabled = false
        }
    },
    methods: {
        SubmitChange(row, Row_Status, message = '成功') {//修改查到的原值并提交
            let _this = this
            let data = {}
            if (this.formData.Purchase_Order_SerialNumber.indexOf("M") != -1) {
                this.purchaseOrderData.Row_Status = '4'
                row.Row_Status = Row_Status//初级
                row.Logistics_Number = this.form.Children.sales_receipt_detail[0].Logistics_Number
                this.purchaseOrderData.Logistics_Number = this.form.Children.sales_receipt_detail[0].Logistics_Number
                row.Logistics_Date = getYMDHMSDate()
                this.purchaseOrderData.Logistics_Date = getYMDHMSDate();
                data = {
                    Data: JSON.stringify({
                        Modify: { delivery_order: [row], purchase_order: [_this.purchaseOrderData] }
                    })
                }
                $.ajax({
                    type: 'POST',
                    url: DomainName + "/getEditAction?token=" + $.cookie("token"),
                    data,
                    success: function (res) {
                        console.log(res)
                        if (res.Status == 'success') {
                            _this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            _this.loading = false
                            window.history.go(-1)
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = absolutepath + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    },
                    dataType: "json"
                });
            } else {
                // this.salesOrderData.Row_Status = '6'
                // row.Row_Status = Row_Status
                // row.Logistics_Number = this.form.Children.sales_receipt_detail[0].Logistics_Number
                // this.salesOrderData.Logistics_Number = this.form.Children.sales_receipt_detail[0].Logistics_Number
                // row.Logistics_Date = getYMDHMSDate()
                // this.salesOrderData.Logistics_Date = getYMDHMSDate();
                data = {
                    Sales_Order_Detail_SerialNumber: this.formData.Purchase_Order_SerialNumber,
                    Type: '6',
                    Logistics: JSON.stringify([{//物流明细
                        disabled1: false,  // 禁用物流公司
                        Sales_Order_Logistics_SerialNumber: '', //主键
                        Sales_Order_SerialNumber: this.formData.Sales_Order_Detail_SerialNumber,   //销售单号
                        LogisticsNumbers: this.form.Children.sales_receipt_detail[0].Logistics_Number,  //物流单号
                        LogisticsCode: '',  //快递公司
                        Quantity: this.form.Children.sales_receipt_detail[0].Receipt_Quantity,   //数量
                    }]),
                    Delivery :JSON.stringify({ Modify: {delivery_order: [row]}}) 
                }
                $.ajax({
                    type: 'POST',
                    url: DomainName + "/setSalesOrderEditType?token=" + $.cookie("token"),
                    data,
                    success: function (res) {
                        console.log(res)
                        if (res.Status == 'success') {
                            _this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            _this.loading = false
                            window.history.go(-1)
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = absolutepath + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    },
                    dataType: "json"
                });
            }
            
        },
        submitForm() {
            if (this.form.Children.sales_receipt_detail[0].Logistics_Numbers == '') {
                this.$message({
                    message: '请填写物流单号',
                    type: 'error'
                });
                return
            }
            let _this = this
            _this.loading = true
            this.form.Receipt_Type = '0'
            this.form.Children.sales_receipt_detail[0].Order_Type = '1'
            this.form.Store_SerialNumber = this.formData.Warehouse_SerialNumber
            this.form.Children.sales_receipt_detail[0].Article_SerialNumber = this.formData.Article_SerialNumber
            console.log(this.form)
            $.ajax({
                type: 'POST',
                url: DomainName + "/setReduceInventoryAction?token=" + $.cookie("token"),
                data: {
                    Data: JSON.stringify({
                        sales_receipt: [_this.form]
                    }),
                    Independence: _this.formData.Independence 
                },
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        _this.SubmitChange(_this.formData, '1', '发货成功')
                    }else{
                        _this.loading = false
                        _this.$message.error(res.Cause)
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        purchaseOrder(Primary_Key) {
            console.log(Primary_Key)
            let _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryIDOriginal?token=" + $.cookie("token"),
                data: {
                    Primary_Key_Name: 'Purchase_Order_SerialNumber',
                    Primary_Key,
                    Data: 'purchase_order',
                    isNullEnterprise: true
                },
                success: function (res) {
                    console.log(res)
                    _this.purchaseOrderData = res[0]
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        salesOrder(Primary_Key) {
            console.log(Primary_Key)
            let _this = this
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryIDOriginal?token=" + $.cookie("token"),
                data: {
                    Primary_Key_Name: 'Sales_Order_Detail_SerialNumber',
                    Primary_Key,
                    Data: 'sales_order_detail',
                    isNullEnterprise: true
                },
                success: function (res) {
                    console.log(res)
                    _this.salesOrderData = res[0]
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        }
    },
})